|
Expression templates is a C++ template metaprogramming technique in which templates are used to represent part of an expression. Typically, the template itself represents a particular kind of operation, while the parameters represent the operands to which the operation applies.〔 〕 The expression template can then be evaluated at a later time, or passed to a function. The technique was invented independently by Todd Veldhuizen and David Vandevoorde.〔〔 〕〔 〕 For example, consider a library representing vectors with a class Vec . It is natural to want to overload operator- and operator so you could write Vec x = alpha where alpha is a scalar and u and v are Vec s. A naive implementation would have operator- and operator return Vec s. However, then the above expression would mean creating a temporary for u-v then another temporary for alpha times that first temporary, then assigning that to x . Even with the return value optimization this will allocate memory at least twice: once for the temporary u-v and once for the result of the overall expression.Expression templates delay evaluation so the expression Vec x = alpha essentially generates at compile time a new Vec constructor. It is as if this constructor takes a scalar and two Vec s by reference; it allocates the necessary memory and then performs the computation. Thus only one memory allocation is performed.An example implementation of expression templates is as follows (using the curiously recurring template pattern as is used by Boost.uBLAS): to essentially with no temporaries needed and only one pass through each memory block. == Libraries using Expression templates == * Boost uBLAS〔 (【引用サイトリンク】 Boost Basic Linear Algebra Library ) 〕 * Eigen〔 (【引用サイトリンク】 Eigen: Lazy Evaluation and Aliasing ) 〕 抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)』 ■ウィキペディアで「expression templates」の詳細全文を読む スポンサード リンク
|